/*
 * @Description:图片上传插件
 * @Version: 2.0
 * @Autor: swc
 * @Date: 2024-01-10 12:41:37
 * @LastEditors: swc
 * @LastEditTime: 2024-01-11 13:41:36
 */

import { ts, Tsrv } from '@/services';

export class ImageUploadAdapter {
  private loader: any;

  private oRichText: any;

  constructor(loader: any, oRichText: any) {
    this.loader = loader;
    this.oRichText = oRichText;
  }

  // [
  //   {
  //     "type": "file",
  //     "sourceFileName": "导出_20240106143817 (1).xlsx",
  //     "targetFileName": "C37B62DA-4E85-4E61-A3C7-F4DCA5CAD760",
  //     "path": "file"
  //   }
  // ]
  upload() {
    return this.loader.file.then(
      (file: File) =>
        new Promise((resolve, reject) => {
          const upd = new ts.T_uploadArg(Tsrv.getWsCliID());
          upd.setType('image').addFile(file);
          upd.SetRouteID(Tsrv.utils.custUUID());
          Tsrv.upLoad(upd).then(([res, err]) => {
            this.oRichText.$msg(err ?? res.msg);
            if (res.success && res.data.length > 0) {
              resolve({
                default: `${this.oRichText.$config.publicURL}${Tsrv.url.download}?fileName=${res.data[0].targetFileName}&path=image`
              });
            }
          });
        })
    );
  }

  // 中止上传
  abort() {
    // 处理axios.cancel()

    this.loader.abort();
  }
}
